from typing import List, Optional


# 定义通道信息基类，方便后续继承
class ChannelInfo:
    def __init__(self, idx_cfg: Optional[int] = None, idx_org: Optional[int] = None, srcRef: Optional[str] = None,
                 **kwargs):
        self.idx_cfg = idx_cfg
        self.idx_org = idx_org
        # 新增 srcRef 属性
        self.srcRef = srcRef
        # 用于存储原始XML属性中没有显式列出的其他属性
        for k, v in kwargs.items():
            setattr(self, k, v)

    def __repr__(self):
        return f"<{self.__class__.__name__} idx_cfg={self.idx_cfg} srcRef='{self.srcRef}'>"


class AnalogChannel(ChannelInfo):
    def __init__(self, type: str, flag: str, au: float, bu: float, sIUnit: str, primary: float, secondary: float,
                 ps: str, freq: Optional[float] = None, p_min: Optional[str] = None, p_max: Optional[str] = None,
                 s_min: Optional[str] = None, s_max: Optional[str] = None, srcRef: Optional[str] = None, **kwargs):
        super().__init__(srcRef=srcRef, **kwargs)
        self.type = type
        self.flag = flag
        self.p_min = p_min if p_min else None
        self.p_max = p_max if p_max else None
        self.s_min = s_min if s_min else None
        self.s_max = s_max if s_max else None
        self.freq = freq
        self.au = au
        self.bu = bu
        self.sIUnit = sIUnit
        self.primary = primary
        self.secondary = secondary
        self.ps = ps


class StatusChannel(ChannelInfo):
    def __init__(self, type: str, flag: str, contact: str, srcRef: Optional[str] = None, **kwargs):
        super().__init__(srcRef=srcRef, **kwargs)
        self.type = type
        self.flag = flag
        self.contact = contact


# --- 设备相关类 ---
class ACVChn:
    def __init__(self, ua_idx: Optional[int] = None, ub_idx: Optional[int] = None, uc_idx: Optional[int] = None,
                 un_idx: Optional[int] = None):
        self.ua_idx = ua_idx
        self.ub_idx = ub_idx
        self.uc_idx = uc_idx
        self.un_idx = un_idx

    def __repr__(self):
        return f"<ACVChn Ua={self.ua_idx}, Ub={self.ub_idx}, Uc={self.uc_idx}, Un={self.un_idx}>"


class ACC_Bran:
    def __init__(self, bran_idx: Optional[int] = None, dir: Optional[str] = None, ia_idx: Optional[int] = None,
                 ib_idx: Optional[int] = None, ic_idx: Optional[int] = None, in_idx: Optional[int] = None):
        self.bran_idx = bran_idx
        self.dir = dir
        self.ia_idx = ia_idx
        self.ib_idx = ib_idx
        self.ic_idx = ic_idx
        self.in_idx = in_idx

    def __repr__(self):
        return f"<ACC_Bran BranIdx={self.bran_idx}, Ia={self.ia_idx}, Ib={self.ib_idx}, Ic={self.ic_idx}, In={self.in_idx}>"


class RX:
    def __init__(self, r1: float, x1: float, r0: float, x0: float):
        self.r1 = r1
        self.x1 = x1
        self.r0 = r0
        self.x0 = x0


class PX:
    def __init__(self, px: float, px0: float):
        self.px = px
        self.px0 = px0


class CG:
    def __init__(self, c1: float, c0: float, g1: Optional[float] = None, g0: Optional[float] = None):
        self.c1 = c1
        self.c0 = c0
        self.g1 = g1
        self.g0 = g0


class Igap:
    def __init__(self, zgap_idx: Optional[int] = None, zsgap_idx: Optional[int] = None):
        self.zgap_idx = zgap_idx
        self.zsgap_idx = zsgap_idx


class Bus:
    def __init__(self, idx: int, bus_name: str, srcRef: Optional[str] = None, sys_ID: Optional[str] = None,
                 VRtg: Optional[float] = None, VRtgSnd: Optional[float] = None, VRtgSnd_Pos: Optional[str] = None):
        self.idx = idx
        self.sys_ID = sys_ID
        self.bus_name = bus_name
        self.srcRef = srcRef
        self.VRtg = VRtg
        self.VRtgSnd = VRtgSnd
        self.VRtgSnd_Pos = VRtgSnd_Pos
        self.acv_chns: List[ACVChn] = []
        self.ana_chns: List[AnalogChannel] = []  # 存储引用，不是原始对象
        self.sta_chns: List[StatusChannel] = []  # 存储引用

    def __repr__(self):
        return f"<Bus idx={self.idx}, name='{self.bus_name}'>"


class Line:
    def __init__(self, idx: int, line_name: str, srcRef: Optional[str] = None, sys_ID: Optional[str] = None,
                 bus_ID: Optional[int] = None, VRtg: Optional[float] = None, ARtg: Optional[float] = None,
                 ARtgSnd: Optional[float] = None, LinLen: Optional[float] = None, bran_num: Optional[int] = None):
        self.idx = idx
        self.sys_ID = sys_ID
        self.line_name = line_name
        self.srcRef = srcRef
        self.bus_ID = bus_ID
        self.VRtg = VRtg
        self.ARtg = ARtg
        self.ARtgSnd = ARtgSnd
        self.LinLen = LinLen
        self.bran_num = bran_num
        self.rx: Optional[RX] = None
        self.px: Optional[PX] = None
        self.cg: Optional[CG] = None
        self.acc_brans: List[ACC_Bran] = []
        self.acv_chns: List[ACVChn] = []  # 存储引用
        self.ana_chns: List[AnalogChannel] = []  # 存储引用
        self.sta_chns: List[StatusChannel] = []  # 存储引用

    def __repr__(self):
        return f"<Line idx={self.idx}, name='{self.line_name}', bus_ID={self.bus_ID}>"


class TransformerWinding:
    def __init__(self, location: str, srcRef: Optional[str] = None, VRtg: Optional[float] = None,
                 ARtg: Optional[float] = None, bran_num: Optional[int] = None, wG: Optional[str] = None):
        self.location = location
        self.srcRef = srcRef
        self.VRtg = VRtg
        self.ARtg = ARtg
        self.bran_num = bran_num
        self.wG = wG
        self.acv_chns: List[ACVChn] = []  # 存储引用
        self.acc_brans: List[ACC_Bran] = []  # 存储引用
        self.igaps: List[Igap] = []

    def __repr__(self):
        return f"<TransformerWinding location='{self.location}', VRtg={self.VRtg}>"


class Transformer:
    def __init__(self, idx: int, trm_name: str, srcRef: Optional[str] = None, sys_ID: Optional[str] = None,
                 pwrRtg: Optional[float] = None, winding_type: Optional[int] = None):
        self.idx = idx
        self.sys_ID = sys_ID
        self.trm_name = trm_name
        self.srcRef = srcRef
        self.pwrRtg = pwrRtg
        self.winding_type = winding_type
        self.windings: List[TransformerWinding] = []
        self.ana_chns: List[AnalogChannel] = []  # 存储引用
        self.sta_chns: List[StatusChannel] = []  # 存储引用

    def __repr__(self):
        return f"<Transformer idx={self.idx}, name='{self.trm_name}'>"
